PEN Statements Example See the ON PEN statement programming example, which uses the PEN function. PEN Statements ---------------------------------------------------------------------------- Action Enable, disable, or suspend lightpen-event trapping. Syntax PEN ON PEN OFF PEN STOP Remarks The PEN ON statement enables lightpen-event trapping. A lightpen event occurs whenever the lightpen is activated by pressing the tip to the screen or pressing the touch ring. If a lightpen event occurs after a PEN ON statement, the routine specified in the ON PEN statement is executed. The PEN OFF statement disables lightpen-event trapping. No trapping takes place until a PEN ON statement is executed. Events occurring while trapping is off are ignored. The PEN STOP statement suspends lightpen-event trapping. No trapping takes place until a PEN ON statement is executed. Events occurring while trapping is suspended are remembered and processed when the next PEN ON statement is executed. However, remembered events are lost if PEN OFF is executed. When a lightpen-event trap occurs (that is, the GOSUB is performed), an automatic PEN STOP is executed so that recursive traps cannot take place. The RETURN statement from the trapping routine automatically performs a PEN ON statement unless an explicit PEN OFF was performed inside the routine. A PEN ON statement must be executed before you use the PEN function. If a Pen function is executed when the lightpen is off, BASIC generates the error message Illegal function call. Note The lightpen requires an IBM Color Graphics Adapter. The PEN statements are not available for OS-2 protected mode. For more information, see Chapter 9, "Event Handling" in the Programmer's Guide. See Also ON event Example The following example uses the PEN statements and the PEN function to enable and display current lightpen status (up or down) and position (x and y coordinates). The ON PEN statement passes control to the PenReport routine when a lightpen event occurs. ' Note. Do not run this program with your mouse driver enabled. CLS ' Clear screen. COLOR 0, 7 ' Set black on white. CLS ' Clear screen. PEN ON ' Enable lightpen ON PEN GOSUB PenReport DO LOCATE 23, 12 PRINT "Press the lightpen against the screen to see a report." LOCATE 24, 17 PRINT " Press the Spacebar to exit the program. "; LOOP UNTIL INKEY$ = " " PEN OFF ' Disable lightpen. COLOR 7, 0 ' Set back to black on white. CLS ' Clean up the screen. END PenReport. DO P = PEN(3) ' Report lightpen status and get X and Y position. LOCATE 10, 27 PRINT "A Pen event has occurred." LOCATE 24, 17 PRINT "Press ANY key to exit the lightpen report."; LOCATE 12, 30 PRINT "lightpen is "; IF P THEN PRINT "Down" X = PEN(4). Y = PEN(5) ELSE PRINT "Up " X = 0. Y = 0 END IF ' Report the X and Y position. LOCATE 14, 22 PRINT "X Position ="; X; " " LOCATE 14, 40 PRINT "Y Position ="; Y; " " LOOP WHILE INKEY$ = "" RETURN